var Order = function (order, user) {
  BaseModel.call(this);
  var self = this;

  var dateFields = ["laterPhaseOne", "laterPhaseTwo", "laterPhaseThr", "laterPhaseFour"];

  var fn = {
    initData: function (order, user) {
      self.id = order.mainId;
      self.detailId = order.purchaseDetailId;
      self.isShowYlts = order.isShowYltsTotalOnPurchase;
      self.curstomer = order.customerBean;
      self.printOrderId = order.purchaseDetailId;

      var fields = ['purchasePeople', 'currency', 'price', 'tax', 'feedTotal', 'orderTotal', 'yltsTotal', 'materialName', 'materialType',
        'materialCode', 'color', 'colorDescription', 'model', 'unit', 'defaultWarehouse', 'procurementCycles', 'orderId', 'companyOrderId',
        'purchaseCount', 'inCount', 'createUser', 'status', 'approvalUser', 'approvalTime', 'createTime', 'providerName', 'purchaseTime',
        'laterPhaseOne', 'laterPhaseTwo', 'laterPhaseThr', 'laterPhaseFour', 'inCountDate', 'tardiness', 'isApprovalUser', 'qualityType',
        'printNeetApproval', 'providerNo', 'remark', 'materialDeptId', 'materialDeptName', 'articleNo', 'factoryNo', 'transmittance',
        'hidePriceForPurchase', 'feedOrderRemark'
      ];
      self.setDataFromObj(order, fields);

      var img = order.img;
      if (SysUtil.isEmpty(img)) {
        img = null;
      }
      self.img = img;

      self.arrearCount = _.round(self.purchaseCount - self.inCount, 2);
      self.totalMoney = this.getTotalMoney();
      var auditableUser = order.auditableUser;
      if (auditableUser !== null && auditableUser.length > 0) {
        self.auditableUser = auditableUser.join(",");
      } else {
        self.auditableUser = '无可审核人';
      }

      if (order.type === 1) {
        var flag = SysUtil.checkApproval(order.specialRemark, {
          lineNo: order.lineNo,
          materialType: order.materialType,
          materialFactory: order.materialFactory,
          isOpraterAdd: order.isOpraterAdd
        });
        if (flag && order.status === 1) {
          self.isApproval = 1;
        } else {
          self.isApproval = 0;
        }
      }

      var isPrint = order.isPrint;
      if (isPrint === null) {
        self.isPrint = 0;
      } else {
        self.isPrint = Number(isPrint);
      }

      var isApprovalUser = self.isApprovalUser;
      var createUserId = order.createUserId;
      self.isCanUpdate = isApprovalUser === 1 || createUserId === user.userId;

      var timeFields = ["createTime", "purchaseTime", "inCountDate", "approvalTime"];
      timeFields.pushArray(dateFields);
      this.formatTimeData(timeFields);
      var laterPhaseDateList = self.getLaterPhaseDateList();
      laterPhaseDateList = _.map(laterPhaseDateList, 'date');
      self.maxLaterPhase = _.maxBy(laterPhaseDateList, function (date) {
        if (date === '') {
          return null;
        }
        return date;
      });

      var feedOrderFeedCount = order.feedOrderFeedCount;
      if (!SysUtil.isEmpty(feedOrderFeedCount)) {
        feedOrderFeedCount = Number(feedOrderFeedCount);
      }
      self.feedOrderFeedCount = feedOrderFeedCount;

      var availableCount = order.availableCount;
      if (!SysUtil.isEmpty(availableCount)) {
        availableCount = Number(availableCount);
      }
      self.availableCount = availableCount;
    },

    formatTimeData(fields) {
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var time = self[field];
        if (time === null || time === '') {
          time = '';
        } else {
          time = moment(time).valueOf();
        }
        self[field] = time;
      }
    },
    getTotalMoney: function () {
      if (self.hidePriceForPurchase === 1) {
        return '';
      }
      var purchaseCount = self.purchaseCount;
      var price = self.price;
      return _.round(purchaseCount * price, globalConfig.pricePrecision);
    }
  };

  this.getLaterPhaseDateList = function () {
    var laterPhaseDateList = [];
    for (var i = 0; i < dateFields.length; i++) {
      var field = dateFields[i];
      var date = self[field];
      if (i !== 0 && date === '') {
        break;
      }

      var obj = {
        date: date,
        readonly: true,
        canDel: false
      };

      if (i === 0 && date === '') {
        obj.readonly = false;
      }

      laterPhaseDateList.push(obj);
    }
    return laterPhaseDateList;
  };

  this.setLaterPhaseDateList = function (laterPhaseDateList) {
    for (var i = 0; i < laterPhaseDateList.length; i++) {
      var laterPhaseDate = laterPhaseDateList[i].date;
      var field = dateFields[i];
      this[field] = laterPhaseDate;
    }
  };

  this.getUpdateObject = function (laterPhaseDateList) {
    var postData = {
      purchaseDetailId: self.printOrderId
    };

    for (var i = 0; i < laterPhaseDateList.length; i++) {
      var laterPhaseDate = laterPhaseDateList[i].date;
      var field = dateFields[i];
      if (laterPhaseDate === '') {
        return field;
      }
      laterPhaseDate = moment(laterPhaseDate).format('YYYY-MM-DD HH:mm:ss');
      postData[field] = laterPhaseDate;
    }
    return postData;
  };

  this.filterData = function (search) {
    var curstomer = this.curstomer;
    if (curstomer === null) {
      curstomerId = '';
    } else {
      curstomerId = curstomer.id;
    }

    var materialName = search.materialName;
    if (materialName === '') {
      return curstomerId === search.curstomerId;
    }
    return this.materialName === materialName && curstomerId === search.curstomerId;
  };

  fn.initData(order, user);
};
